package com.yy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.entity.Category;
import com.yy.entity.Dish;
import com.yy.entity.Setmeal;
import com.yy.mapper.CategoryMapper;
import com.yy.service.CategoryService;
import com.yy.service.DishService;
import com.yy.service.SetmealService;
import com.yy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * create by KongFuning on 2022/7/31
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /*
    * 分页
    * */
    @Override
    public R pageCategory(Integer page, Integer pageSize) {
        Page<Category> categoryPage = new Page(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Category::getSort);
        page(categoryPage,queryWrapper);
        return R.success(categoryPage);
    }

    /*
     * 新增菜品/套餐分类
     * */
    @Override
    public R saveCategory(Category category) {
        // 判断分类是否已经存在
        Category cate = query().eq("name", category.getName()).one();
        if(cate != null){
            // 表示该分类已存在，提示错误信息
            return R.error("分类：【" + category.getName() + "】已存在！");
        }
        return R.success(category);
    }

    /*
     * 修改分类
     * */
    @Override
    public R updateCategory(Category category) {
        updateById(category);
        return R.success("修改分类成功！");
    }

    /*
    * 删除分类
    * */
    @Override
    public R deleteCategory(String ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据分类id查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        long count1 = dishService.count(dishLambdaQueryWrapper);
        // 判断该分类是否关联了菜品，如果已经关联，抛出一个业务异常
        if(count1 > 0){
            // 已经关联了菜品
            return R.error("当前分类下关联了菜品，不能删除！");
        }
        // 判断该分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据分类id查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        long count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            // 已经关联了套餐
            return R.error("当前分类下关联了套餐，不能删除！");
        }
        removeById(ids);
        return R.success("删除分类成功！");
    }

    /*
     * 添加菜品时回显菜品分类
     * */
    @Override
    public R categoryList(Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = list(queryWrapper);
        return R.success(list);
    }
}
